/*
 * @Author: huangrongji
 * @Date: 2021-01-29 23:33:24
 * LastEditors: 胡皇
 * LastEditTime: 2021-03-28 18:21:20
 * @Description: file content
 * @FilePath: \news_testd:\工作空间\workspace\repair\service\api.js
 */
const GET = 'GET';
const POST = 'POST';
const PUT='PUT'
const baseURL = 'https://css.light-year-tech.com/';

// request封装
function request(method, url, data) {
    return new Promise(function(resolve, reject) {
        let header = {
            'content-type': 'application/json',
            openid: wx.getStorageSync('openid'),
            token: wx.getStorageSync('token')
        };
        wx.showLoading({
            title: '加载中',
            mask: true
        });

        wx.request({
            url: baseURL + url,
            method: method,
            data: method === POST ? JSON.stringify(data) : data,
            header: header,
            success(res) {
                //请求成功
                //判断状态码---statusCode状态码判断
                if (res.statusCode === 200) {
                    resolve(res.data);
                    wx.hideLoading();
                } else if (res.statusCode == 401) {
                    wx.hideLoading();
                    wx.clearStorage();
                    wx.navigateTo({
                        url: '/pages/user/login/login'
                    });
                } else {
                    //其他异常
                    reject(res.statusCode);
                    wx.showToast({ title: '系统错误' });
                }
            },
            fail(err) {
                wx.showToast({ title: '系统错误' });
                //请求失败
                reject(err);
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    });
}

// 接口
const API = {
    //  登录并获取手机号、CosKey统一接口
    getOpenid: (data) => request(POST, `wx-login/login`, data),
    // 只获取手机号接口
    getPhone: (data) => request(POST, `wx-login/phone`, data),
    // 单独获取临时CosKey
    getCosKey: (data) => request(GET, `wx-login/getCosKey`, data),
    // 获取报修页子问题的接口
    getRepairType: (type, data) => request(GET, `order-repair/type/${type}`, data),
    //获取所有报修单
    //getAllorder:(data)=>request(GET,`order-info/openid/${wx.getStorageSync('openid')}?openid=${wx.getStorageSync('openid')}`+data)
    //测试用
    getAllorder: (data) => request(GET, `order-info/openid/${data}`),

    submitOrder: (data) => request(POST, `order-repair/addorder`, data),
    updateOrder: (data) => request(POST, `order-info/update`, data),
    unrealdel:(type,data)=>request(PUT,`order-info/unrealdel/${type}`,data),
    //https://css.light-year-tech.com/feedback/add
    feedback:(data)=>request(POST,`feedback/add`,data),
   



};
// 导出
module.exports = {
    API: API
};